package com.freedo.demo.domain.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import com.freedo.demo.domain.i.IUserDao;
import com.freedo.demo.entity.User;

@Repository(value="mongoUserDao")
public class MongoUserDao implements IUserDao {

	@Autowired
	private MongoTemplate mongoTemplate;
	
	
	/**
	 * 插入一条用户数据
	 */
	@Override
	public int insertUser(User u) {
		// TODO Auto-generated method stub
		mongoTemplate.insert(u);
		return 0;
	}

	
	@Override
	public int getUserCount() {
		// TODO Auto-generated method stub
		Criteria c = new Criteria();
		long count = mongoTemplate.count(Query.query(c), User.class);
		return (int)count;
	}

	@Override
	public User getUser(String id) {
		// TODO Auto-generated method stub
		return mongoTemplate.findById(id, User.class);
	}

	@Override
	public List<User> getUsers() {
		// TODO Auto-generated method stub
		return mongoTemplate.findAll(User.class);
	}

	@Override
	public List<User> getUsersPage(int offset, int pagecount) {
		// TODO Auto-generated method stub
		Criteria c = new Criteria();
		return mongoTemplate.find(Query.query(c).skip(offset).limit(pagecount), User.class);
	}

}
